package com.clkj.xiuzheng.controller;

import com.clkj.xiuzheng.pojo.Product;
import com.clkj.xiuzheng.pojo.ProductInfo;
import com.clkj.xiuzheng.pojo.ProductStock;
import com.clkj.xiuzheng.pojo.common.Result;
import com.clkj.xiuzheng.pojo.common.StatusCode;
import com.clkj.xiuzheng.service.ProductService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/product")
@Api(value = "/product",tags = {"商品管理"})
@CrossOrigin
public class ProductController {
    @Autowired
    ProductService productService;

    @ApiOperation(value = "添加商品")
    @PostMapping("/addProduct")
    public Result addProduct(Product product, HttpServletRequest request){
        try {
              productService.addProduct(product,request);
            return new Result(true, StatusCode.OK,"添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"添加失败");
        }
    }
    @ApiOperation(value = "删除商品")
    @PostMapping("/deleteProduct")
    public Result deleteProduct(@RequestParam("productId") Integer productId){
        try {
            productService.deleteProduct(productId);
            return new Result(true, StatusCode.OK,"删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"删除失败");
        }
    }
    @ApiOperation(value = "获取商品列表(有分页)")
    @PostMapping("/listProduct")
    public Result listProduct(@RequestParam Integer pageSize,
                              @RequestParam Integer pageNum,
                              Integer companyId){
        try {
            PageInfo pageInfo = productService.listProduct(pageSize, pageNum,companyId);
            return  new Result(true, StatusCode.OK,"查询成功",pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"获取失败");
        }
    }
    @ApiOperation(value = "获取商品列表(无分页)")
    @PostMapping("/listProductAll")
    public Result listProductAll(Integer companyId){
        try {
           List<Product> list = productService.listProductAll(companyId);
            return  new Result(true, StatusCode.OK,"查询成功",list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"获取失败");
        }
    }

    @ApiOperation(value = "修改商品")
    @PostMapping("/updateProduct")
    public Result updateProduct(@RequestBody Product product){
        try {
            productService.updateProduct(product);
            return new Result(true, StatusCode.OK,"修改成功");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"修改失败");
        }
    }
    @ApiOperation(value = "查询商品")
    @PostMapping("/queryProduct")
    public Result queryProduct(@RequestParam(value = "productName",required = false) String productName){
        try {
            List<ProductInfo> product = productService.queryProduct(productName);
            return new Result(true, StatusCode.OK,"查询成功",product);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"查询失败");
        }
    }

    @ApiOperation(value = "商品详情")
    @PostMapping("/productDetail")
    public Result productDetail(@RequestParam("productId") Integer productId){
        try {
            ProductInfo productinfo = productService.productDetail(productId);
            return new Result(true, StatusCode.OK,"查询成功",productinfo);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"查询失败");
        }
    }

    @ApiOperation(value = "手机端获取在售商品")
    @PostMapping("/listAllProduct")
    public Result listAllProduct(@ApiParam(name="companyId",value="公司Id") @RequestParam Integer companyId){
        try {
            List<ProductStock> stocks  = productService.listAllProduct(companyId);
            return  new Result(true, StatusCode.OK,"查询成功",stocks);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"获取失败");
        }
    }

    @ApiOperation(value = "查询商品")
    @PostMapping("/updateProductStatus")
    public Result updateProductStatus(Integer productId,Integer status){
        try {
            productService.updateProductStatus(productId,status);
            return new Result(true, StatusCode.OK,"查询成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"查询失败");
        }
    }



}
